/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package interfaz;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

/**
 *
 * @author Arkan
 */
public class DialogoBuscarFotos extends JDialog implements ActionListener
{
    private final static String BUSCAR_POR_ID = "Buscar por id";
    
    private final static String BUSCAR_POR_PERSONAS = "Buscar por personas";
    
    private final static String ACEPTAR = "Aceptar";
    
    private final static String CANCELAR = "Cancelar";
    
    private InterfazFotosP2P principal;
    
    private JRadioButton radBuscarID;
    
    private JRadioButton radBuscarPersonas;
    
    private ButtonGroup grupo;
    
    private JTextField txtID;
    
    private JTextField txtPersonas;
    
    private JButton butAceptar;
    
    private JButton butCancelar;
    
    public DialogoBuscarFotos( InterfazFotosP2P ventana )
    {
        principal = ventana;
        
        setTitle( "Buscar Fotos" );
        setSize( 350, 110 );
        setDefaultCloseOperation( DISPOSE_ON_CLOSE );
        setResizable( false );
        setModal( true );
        setLocationRelativeTo( principal );
        setLayout( new GridLayout( 3, 2 ) );
        
        radBuscarID = new JRadioButton( "ID" );
        radBuscarID.setActionCommand( BUSCAR_POR_ID );
        radBuscarID.addActionListener( this );
        radBuscarID.setSelected( true );
        add( radBuscarID );
        
        txtID = new JTextField( );
        add( txtID );
        
        radBuscarPersonas = new JRadioButton( "Personas" );
        radBuscarPersonas.setActionCommand( BUSCAR_POR_PERSONAS );
        radBuscarPersonas.addActionListener( this );
        add( radBuscarPersonas );
        
        grupo = new ButtonGroup( );
        grupo.add( radBuscarID );
        grupo.add( radBuscarPersonas );
        
        txtPersonas = new JTextField( );
        txtPersonas.setEditable( false );
        add( txtPersonas );
        
        butAceptar = new JButton( ACEPTAR );
        butAceptar.setActionCommand( ACEPTAR );
        butAceptar.addActionListener( this );
        add( butAceptar );
        
        butCancelar = new JButton( CANCELAR );
        butCancelar.setActionCommand( CANCELAR );
        butCancelar.addActionListener( this );
        add( butCancelar );
    }
    
    public void actionPerformed( ActionEvent e )
    {
        String comando = e.getActionCommand( );
        
        if( comando.equals( BUSCAR_POR_ID ) )
        {
            txtID.setEditable( true );
            txtPersonas.setEditable( false );
        }
        else if( comando.equals( BUSCAR_POR_PERSONAS ) )
        {
            txtID.setEditable( false );
            txtPersonas.setEditable( true );
        }
        else if( comando.equals( ACEPTAR ) )
        {
            if( radBuscarID.isSelected( ) )
            {
                if( !txtID.getText( ).equals( "" ) )
                {
                    principal.buscarPorID( txtID.getText( ) );
                    dispose( );
                }
            }
            else
            {
                if( radBuscarPersonas.isSelected( ) )
            {
                if( !txtPersonas.getText( ).equals( "" ) )
                {
                    principal.buscarPorPersonas( txtPersonas.getText( ) );
                    dispose( );
                }
            }
            }
        }
        else if( comando.equals( CANCELAR ) )
        {
            dispose( );
        }
    }
}
